Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
¿Por qué mi uso de la función de reducción de javascript devuelve "indefinido"?

1) Tengo una serie de objetos, llamados empleados.

2) Estoy tratando de encontrar al empleado mejor pagado de la matriz.

3) Estoy usando la función de reducción de Javascript (función interna findHighestPaid) para hacerlo.

4) Ejecutar findHighestPaid(employees) devuelve "indefinido" cuando esperaba que devolviera el objeto que tiene el nombre de propiedad: Abelard.

Aquí está mi código.

 const employees = [ { name: "Aziz", salary: 17050, }, { name: "Angela", salary: 17060, }, { name: "Abelard", salary: 17070, }, ]; function findHighestPaid(arrayOfObjects) { let myObject = { name: "pretendEmployee", salary: 17040, }; arrayOfObjects.reduce(myFunction, myObject); function myFunction(acc, item) { let personSalary = item.salary; let accSalary = acc.salary; if (Math.sign(personSalary - accSalary) > 0) { console.log(`returning object for ${item.name}`); return item; } else { console.log(`returning object for ${acc.name}`); return acc; } } // end myFunction }

Cuando ejecuto findHighestPaid(employees) y miro en la consola, veo:

objeto de retorno para Aziz // (como esperaba)

objeto de retorno para Angela // (como esperaba)

objeto de retorno para Abelard // (como esperaba)

indefinido // ( oh oh!!! )

¿Por qué la función de reducción devuelve "indefinido"?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

lo undefined que se imprime al final es el valor de retorno de la función findHighestPaid() que ejecuta.

ya que no está devolviendo nada, está devolviendo undefined . si su objetivo es ver el valor de retorno de reduce() agregue una declaración de retorno a la función como esta:

 const employees = [ { name: "Aziz", salary: 17050, }, { name: "Angela", salary: 17060, }, { name: "Abelard", salary: 17070, }, ]; function findHighestPaid(arrayOfObjects) { let myObject = { name: "pretendEmployee", salary: 17040, }; return arrayOfObjects.reduce(myFunction, myObject); function myFunction(acc, item) { let personSalary = item.salary; let accSalary = acc.salary; if (Math.sign(personSalary - accSalary) > 0) { console.log(`returning object for ${item.name}`); return item; } else { console.log(`returning object for ${acc.name}`); return acc; } } // end myFunction } console.log(findHighestPaid(employees));

about 4 years ago · Juan Pablo Isaza Report

0

Su código funciona perfectamente, aunque no necesita Math.sign en absoluto, solo return el resultado:

 const employees = [ { name: "Aziz", salary: 17050, }, { name: "Angela", salary: 17080, }, { name: "Abelard", salary: 17070, }, ]; function findHighestPaid(arrayOfObjects) { let myObject = { name: "pretendEmployee", salary: 17040, }; return arrayOfObjects.reduce(myFunction, myObject); function myFunction(acc, item) { let personSalary = item.salary; let accSalary = acc.salary; if (personSalary - accSalary > 0) { console.log(`returning object for ${item.name}`); return item; } else { console.log(`returning object for ${acc.name}`); return acc; } } // end myFunction } console.log('Highest paid', findHighestPaid(employees));

about 4 years ago · Juan Pablo Isaza Report

0

solo tienes que ponerlo asi

 function findHighestPaid(arrayOfObjects) { return arrayOfObjects.reduce((prev, curr) => { if(!prev) { return curr; } return curr.salary > prev.salary ? curr : prev; }, null); } const highestPaidEmployee = findHighestPaid(employees);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!